// 格式检验工具类
// const tipUtils = require('../../../utils/tip.js'); //提示模块引入

//制保留2位小数，如：2，会在2后面补上00.即2.00 
function toDecimal(x) {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}

// 字符串是否为空
function isNULL(data) {
  var data = data;
  if (data == null || data === undefined || data == "" || data == '' || !data || data.length == 0 || !data.length) {
    return true;
  }
  return false;
}

// 对象是否为空
function isEmptyObject(obj) {
  for (var name in obj) {
    return false;//返回false，不为空对象
  }
  return true;//返回true，为空对象
}

// 验证码手机号规则
function isCheckTel(telNo) {
  if (!(/^1[0-9][0-9]\d{8}$/.test(telNo))) {
    return false;
  }
  return true;
}

//验证身份证规则
function isCardNo(card) {
  // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (reg.test(card) === false) {
    // alert("身份证输入不合法");
    return false;
  } else {
    return true;
  }
}

//时间格式化
function formatDate(date, format) {
  var v;
  if (typeof date == "string" || typeof date != "object") {
    return;
  }
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  var weekDay = date.getDay();
  var ms = date.getMilliseconds();
  var weekDayString = "";

  if (weekDay == 1) {
    weekDayString = "星期一";
  } else if (weekDay == 2) {
    weekDayString = "星期二";
  } else if (weekDay == 3) {
    weekDayString = "星期三";
  } else if (weekDay == 4) {
    weekDayString = "星期四";
  } else if (weekDay == 5) {
    weekDayString = "星期五";
  } else if (weekDay == 6) {
    weekDayString = "星期六";
  } else if (weekDay == 0) {
    weekDayString = "星期日";
  }
  v = format;
  //Year 
  v = v.replace(/yyyy/g, year);
  v = v.replace(/YYYY/g, year);
  v = v.replace(/yy/g, (year + "").substring(2, 4));
  v = v.replace(/YY/g, (year + "").substring(2, 4));

  //Month 
  var monthStr = ("0" + month);
  v = v.replace(/MM/g, monthStr.substring(monthStr.length - 2));

  //Day 
  var dayStr = ("0" + day);
  v = v.replace(/dd/g, dayStr.substring(dayStr.length - 2));

  //hour 
  var hourStr = ("0" + hour);
  v = v.replace(/HH/g, hourStr.substring(hourStr.length - 2));
  v = v.replace(/hh/g, hourStr.substring(hourStr.length - 2));

  //minute 
  var minuteStr = ("0" + minute);
  v = v.replace(/mm/g, minuteStr.substring(minuteStr.length - 2));

  //Millisecond 
  v = v.replace(/sss/g, ms);
  v = v.replace(/SSS/g, ms);

  //second 
  var secondStr = ("0" + second);
  v = v.replace(/ss/g, secondStr.substring(secondStr.length - 2));
  v = v.replace(/SS/g, secondStr.substring(secondStr.length - 2));

  //weekDay 
  v = v.replace(/E/g, weekDayString);
  return v;
};
// 输入密码验证
function ispassword(obj, fun) {

  var password = obj;
  var num = 0;
  var number = 0;
  var letter = 0;
  var bigLetter = 0;
  var chars = 0;
  var text = '';

  if (password.search(/[0-9]/) != -1) {
    num += 1;
    number = 1;
  };
  if (password.search(/[A-Z]/) != -1) {
    num += 1;
    bigLetter = 1;
  }
  if (password.search(/[a-z]/) != -1) {
    num += 1;
    letter = 1;
  }
  if (password.search(/[^A-Za-z0-9]/) != -1) {
    num += 1;
    chars = 1;
  }
  if (num == 1) {
    if (number == 1) {
      text = "不能全为数字"
    }
    if (letter == 1) {
      text = "不能全为字母"
    }
    if (bigLetter == 1) {
      text = "不能全为字母"
    }
    if (chars == 1) {
      text = "不能全为字符"
    }

  }

  if (typeof fun == "function") {
    fun(text);
  }
}

module.exports = {
  isNULL: isNULL, //校验字符串是否为空
  isEmptyObject: isEmptyObject, //校验对象是否为空
  formatDate: formatDate, //时间格式化
  isCheckTel: isCheckTel, //校验手机号码规则
  ispassword: ispassword, // 输入密码验证
  toDecimal: toDecimal, //制保留2位小数，如：2，会在2后面补上00.即2.00 
  isCardNo: isCardNo, //校验身份证规则
}
